package com.zpark.servlet_context;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @PackageName: com.zpark.servlet_context
 * @ClassName: ServletContextDemo01
 * @Description:
 * @author: RZS
 * @date: 2023/3/8  8:50
 */
@WebServlet("/sc01.do")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 获取ServletContext对象的四种方法
         */
        // 1、通过 GenericServlet的getServletContext方法获取ServletContext对象
        ServletContext context = super.getServletContext();
        // 2、通过ServletConfig对象获取
        ServletConfig config = super.getServletConfig();
        ServletContext context1 = config.getServletContext();
        // 3、通过session对象获取
        HttpSession session = req.getSession();
        ServletContext context2 = session.getServletContext();
        // 4、通过请求对象获取
        ServletContext context3 = req.getServletContext();

        System.out.println(context);
    }
}
